package com.huanggf.meituan.order;


import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.huanggf.meituan.R;
import com.huanggf.meituan.fooddetail.FoodDetails;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;

public class OrderAdapter extends BaseAdapter {
    private List<Order> orders;
    private Context context;

    public OrderAdapter(Context context, List<Order> orders) {
        this.context = context;
        this.orders = orders;
    }

    @Override
    public int getCount() {
        return orders.size();
    }

    @Override
    public Object getItem(int position) {
        return orders.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.order_layout_item, null);
            viewHolder.shop_logo = (ImageView) convertView.findViewById(R.id.shop_logo);
            viewHolder.shop_name = (TextView) convertView.findViewById(R.id.shop_name);
            viewHolder.flag = (TextView) convertView.findViewById(R.id.flag);
            viewHolder.reserve = (TextView) convertView.findViewById(R.id.reserve);
            viewHolder.reserve_time = (TextView) convertView.findViewById(R.id.reserve_time);
            viewHolder.discount1 = (TextView) convertView.findViewById(R.id.discount1);
            viewHolder.discount2 = (TextView) convertView.findViewById(R.id.discount2);
            viewHolder.discount3 = (TextView) convertView.findViewById(R.id.discount3);
            viewHolder.discount4 = (TextView) convertView.findViewById(R.id.discount4);
            viewHolder.order_merchandise = (FrameLayout) convertView.findViewById(R.id.order_merchandise);
            viewHolder.repay = (Button) convertView.findViewById(R.id.repay);
            viewHolder.comment = (Button) convertView.findViewById(R.id.comment);
            Order order = orders.get(position);
            try {
                FoodDetails foodDetails = order.getFoodDetails();
                if (null != foodDetails.getShopLogo() && !foodDetails.getShopLogo().trim().equals(""))
                    viewHolder.shop_logo.setImageBitmap(BitmapFactory.decodeStream(new FileInputStream(foodDetails.getShopLogo())));
                viewHolder.shop_name.setText(foodDetails.getShopName());
                viewHolder.discount1.setText(foodDetails.getDiscount1());
                viewHolder.discount2.setText(foodDetails.getDiscount2());
                viewHolder.discount3.setText(foodDetails.getDiscount3());
                viewHolder.discount4.setText(foodDetails.getDiscount4());
                //order
                viewHolder.flag.setText(order.getFlag());
                if (null != order.getReserve()) {
                    viewHolder.reserve.setText(order.getReserve());
                    viewHolder.reserve_time.setText(order.getReserve_time()+"起送");
                } else {
                    viewHolder.reserve.setText("");
                    viewHolder.reserve.setBackgroundColor(Color.WHITE);
                    viewHolder.reserve_time.setText("");
                    viewHolder.reserve_time.setBackgroundColor(Color.WHITE);
                }
                //merchandise
                List<Merchandise> merchandises = order.getOrderMerchandises();
                if (null != merchandises && merchandises.size() > 0) {
                    if (merchandises.size() > 1) {
                        View order_layout_item_merchandise2 = LayoutInflater.from(context).inflate(R.layout.order_layout_item_merchandise2, null);
                        ImageView m_img1 = (ImageView) order_layout_item_merchandise2.findViewById(R.id.m_img1);
                        TextView m_name1 = (TextView) order_layout_item_merchandise2.findViewById(R.id.m_name1);
                        ImageView m_img2 = (ImageView) order_layout_item_merchandise2.findViewById(R.id.m_img2);
                        TextView m_name2 = (TextView) order_layout_item_merchandise2.findViewById(R.id.m_name2);
                        ImageView m_img3 = (ImageView) order_layout_item_merchandise2.findViewById(R.id.m_img3);
                        TextView m_name3 = (TextView) order_layout_item_merchandise2.findViewById(R.id.m_name3);
                        TextView total_price = (TextView) order_layout_item_merchandise2.findViewById(R.id.total_price);
                        TextView purchase_count = (TextView) order_layout_item_merchandise2.findViewById(R.id.purchase_count);
                        double total_price_value = 0.0;
                        for (int i = 0; i < merchandises.size(); i++) {
                            Merchandise merchandise = merchandises.get(i);
                            if (i == 0) {
                                if (null != merchandise.getM_img_path() && !merchandise.getM_img_path().trim().equals(""))
                                    m_img1.setImageBitmap(BitmapFactory.decodeStream(new FileInputStream(merchandise.getM_img_path())));
                                m_name1.setText(merchandise.getM_name());
                            } else if (i == 1) {
                                if (null != merchandise.getM_img_path() && !merchandise.getM_img_path().trim().equals(""))
                                    m_img2.setImageBitmap(BitmapFactory.decodeStream(new FileInputStream(merchandise.getM_img_path())));
                                m_name2.setText(merchandise.getM_name());
                            } else if (i == 2) {
                                if (null != merchandise.getM_img_path() && !merchandise.getM_img_path().trim().equals(""))
                                    m_img3.setImageBitmap(BitmapFactory.decodeStream(new FileInputStream(merchandise.getM_img_path())));
                                m_name3.setText(merchandise.getM_name());
                            }
                            //总价
                            total_price_value+=merchandise.getPrice();
                        }
                        total_price.setText(String.valueOf(total_price_value));
                        purchase_count.setText(String.valueOf(merchandises.size()));
                        viewHolder.order_merchandise.addView(order_layout_item_merchandise2);
                    } else {
                        Merchandise merchandise = merchandises.get(0);
                        View order_layout_item_merchandise1 = LayoutInflater.from(context).inflate(R.layout.order_layout_item_merchandise1, null);
                        ImageView m_img = (ImageView) order_layout_item_merchandise1.findViewById(R.id.m_img);
                        TextView m_name = (TextView) order_layout_item_merchandise1.findViewById(R.id.m_name);
                        TextView total_price = (TextView) order_layout_item_merchandise1.findViewById(R.id.total_price);
                        TextView purchase_count = (TextView) order_layout_item_merchandise1.findViewById(R.id.purchase_count);

                        if (null != merchandise.getM_img_path() && !merchandise.getM_img_path().trim().equals(""))
                            m_img.setImageBitmap(BitmapFactory.decodeStream(new FileInputStream(merchandise.getM_img_path())));

                        m_name.setText(merchandise.getM_name());
                        total_price.setText("￥" + merchandise.getPrice());
                        purchase_count.setText("1");
                        viewHolder.order_merchandise.addView(order_layout_item_merchandise1);
                    }
                }
            } catch (FileNotFoundException e) {
                Log.e("FileNotFoundException", e.getMessage());
            }
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        return convertView;
    }

    private class ViewHolder {
        private ImageView shop_logo;
        private TextView shop_name;
        private TextView flag;
        private TextView reserve;
        private TextView reserve_time;
        private TextView discount1;
        private TextView discount2;
        private TextView discount3;
        private TextView discount4;
        private FrameLayout order_merchandise;
        private Button repay;
        private Button comment;
    }
}
